# coding:utf-8
# 登陆 在线管理

import glog, glb
import account_mgr, role_mgr, car_mgr

# 在线数据 {session_key : [account, role_id]}
online_data = {}

_next_id = 1

# 初始化
def init():
	print "login>init"


def get_account_and_role_id (session_key):
	return online_data.get(session_key, None)


def is_online (session_key):
	return online_data.has_key(session_key)


def _get_session_key_by_account (account):
	for k, [acc, _] in online_data.iteritems():
		if acc == account:
			return k


def on_login (data):
	print "on_login", data
	
	account = data[0]
	password = data[1]

	if not account_mgr.check_password(account, password):
		glog.log("login>login FEILED")
		return glb.return_err('帐号或者密码错误，无法登录')

	session_key = _get_next_id()
	role_id = None

	role = role_mgr.get_one_role_by_account(account)
	if role:
		glog.log("login>login success, have role")
		role_id = role.id
	else:
		glog.log("login>login success, no role, auto createreturn None")
		# 自动创建角色
		role_name = "not_named"
		role_race = 0
		role_id = _create_role(account, role_name, role_race)
		if not role_id:
			return glb.return_err("自动创建角色失败")

	online_data[session_key] = [account, role_id]  # 记录在线
	return glb.return_ok([session_key, role_id])


def _get_next_id ():
	global _next_id
	res = _next_id
	_next_id += 1
	return res

"""
def on_role_list (data):
	print "on_role_list", data

	conn_id = data[0]

	# 检测连接是否登录
	if not check_conn_logined(conn_id): return

	acc = conn_acc_map[conn_id]
	roles = account_mgr.get_account_roles(acc)
	data = []
	for rid, (idx, name) in roles.iteritems():
		data.append(rid)
		data.append(name)

	# msgid, [ 角色id, 角色名字 ]
	gnet.send( conn_id, [ prt_login.ROLE_LIST, data ] )
"""
"""
def on_select_role (data):
	print "on_select_role", data

	conn_id = data[0]
	role_id = data[1]

	# 检测连接是否登录
	if not check_conn_logined(conn_id): return
	
	# 选择玩家之后, 设置登陆数据
	account = get_account_by_connect_id(conn_id)
	player_mgr.set_login_data(conn_id, account, role_id)

	# msgid, isOk
	gnet.send( conn_id, [ prt_login.SELECT_ROLE, 1 ] )
"""

# 创建帐号
def on_create_account (data):
	print "on_create_account", data
	acc = data[0]
	pwd = data[1]

	if account_mgr.create_account(acc, pwd):
		glog.log("login>create_account success, account:%s" % acc)
		return glb.return_ok()
	else:
		glog.log("login>create_account FEILED, account:%s" % acc)
		return glb.return_err('输入的帐号无效')

"""
# 创建角色
def on_create_role (data, account, role):
	print "on_create_role", data
	role_name = data[0]
	role_class = data[1]

	if account is None:
		return glb.return_cmd('relogin') # 重新登录
	
	session_key = _get_session_key_by_account(account)

	# 数据库创建角色
	if role_mgr.create_role(account, role_name, role_class):
		# 读取出来
		role = role_mgr.get_one_role_by_account(account)
		if role:
			online_data[session_key][1] = role.id  # 记录在线的角色
			return glb.return_ok(None, 'main_scene') 
		else:
			return glb.return_err('创建角色失败1')
	else:
		return glb.return_err('创建角色失败')
"""

# 创建角色
def _create_role (account, role_name, role_race):
	# 数据库创建角色
	if not role_mgr.create_role(account, role_name, role_race):
		return None
	
	# 读取出来
	role = role_mgr.get_one_role_by_account(account)
	if not role:
		return None

	# Role如果创建成功，一般不会出错
	car_mgr.add(role.id, 1)
	car_mgr.add(role.id, 1)

	return role.id



"""
# 删除角色
def on_delete_role (data):
	print "on_delete_role", data

	conn_id = data[0]
	role_id = data[1]
	
	# 检测连接是否登录
	if not check_conn_logined(conn_id): return
		
	account = conn_acc_map[conn_id]
	res = player_mgr.delete_role(account, role_id)
	
	if res:
		# msgid, isOk
		gnet.send( conn_id, [ prt_login.DELETE_ROLE, 1 ] )		
	else:
		gnet.send( conn_id, [ prt_login.DELETE_ROLE, 0 ] )
"""


"""
# 连接是否登录
def check_session_logined (session):
	if not is_conn_logined(conn_id):
		gnet.send( conn_id, [ prt_login.ERROR, "连接未登录" ] )
		return False
	return True


# 连接是否登录
def is_conn_logined (conn_id):
	return conn_id in conn_acc_map


# 获取conn_id 和 账号的对应关系
def get_connect_id_by_account (account):
	return acc_conn_map.get(account)


def get_account_by_connect_id (conn_id):
	return conn_acc_map.get(conn_id)


"""